<template>
	<view>
		<u-navbar
			title="评价"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<scroll-view @scrolltolower="onLower" :scroll-y="true" :style="{height: scrollViewHeight}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton avatarSize="48" avatarShape="square" style="margin-bottom: 32rpx;" :loading="loading" avatar rows="3" v-for="i in 8" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else-if="!loading && list.length === 0">
				<u-empty
				    mode="data"
					icon="http://cdn.uviewui.com/uview/empty/data.png"
				>
				</u-empty>
			</template>
			<template v-else-if="!loading && list.length > 0">
				<view class="shop-review">
					<view class="shop-tuijian-title">
						<view style="display: flex;align-items: center;font-weight: bold;">
							<text>评价</text>
						</view>
						<view style="display: flex;align-items: center;font-size: 24rpx;color: #666;">
							<text>共{{reviewTotal >= 99 ? '99+' : reviewTotal}}条评价</text>
						</view>
					</view>
					<template v-for="(review, index2) in list">
						<view class="shop-review-title" :key="review.id">
							<view style="display: flex;align-items: center;">
								<u--image shape="circle" mode="aspectFill" :src="review.avatar" width="48rpx" height="48rpx"></u--image>
								<text style="padding: 0 8rpx;font-size: 24rpx;line-height: 24rpx;color: #333;">{{review.memberName}}</text>
								<u-rate :readonly="true" :activeColor="appColor" gutter="0" size="14" :count="5" :allowHalf="true" :value="review.score"></u-rate>
							</view>
							<text style="color: #666;font-size: 24rpx;line-height: 24rpx;">{{review.createTime.slice(0, 10)}}</text>
						</view>
						<view class="u-line-2" style="font-size: 28rpx;color: #000000;margin: 16rpx 0;">{{review.content}}</view>
						<u-scroll-list style="padding-bottom: 0;" :indicatorWidth="0" :enable-flex="true" :indicatorActiveColor="appColor">
							<view class="scroll-list" style="flex-direction: row;">
								<view @click="previewImg(index, review.image)"  :style="{marginRight: review.image.length - 1 === index ? 0 : '16rpx'}" class="tj-item" v-for="img in review.image" :key="img.id">
									<u--image radius="16rpx" mode="aspectFill" :src="img.url" width="160rpx" height="160rpx"></u--image>
								</view>
							</view>
						</u-scroll-list>
						<view class="u-border-bottom" v-if="index2 !== list.length-1"/>
					</template>
				</view>
				<u-loadmore :status="status" />
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import {goodsCommentList} from '@/api/shop.js';
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				list: [],
				loading: true,
				status: 'loadmore',
				pageNo: 1,
				pageSize: 6,
				goodsId: undefined,
				reviewTotal: 0
			}
		},
		computed: {
			...mapState(['appInfo']),
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 + 'px';
			}
		},
		onLoad({goodsId}) {
			this.goodsId = goodsId;
			this.getList();
		},
		methods: {
			// 预览图片
			previewImg(current, urls) {
				urls = urls.map(item => item.url);
				uni.previewImage({
					current,
					urls,
				});
			},
			getList() {
				if(this.pageNo === 1) {
					this.loading = true;
				}
				let postObj = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					goodsId: this.goodsId
				}
				goodsCommentList(postObj).then(res => {
					const {current, pages, records, total} = res.result;
					this.reviewTotal = total;
					if(this.pageNo === 1) {
						let empty = [];
						this.list = empty.concat(records);
					} else {
						this.list = this.list.concat(records);
					}
					if(current >= pages) {
						this.status = 'nomore'
					} else {
						this.status = 'loading'
						this.pageNo ++;
					}
				}).finally(() => {
					this.loading = false;
				})
			},
			onLower() {
				if(this.status === 'loading') {
					this.getList();
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>

<style lang="scss" scoped>
	.shop-review {
		padding: 32rpx;
		padding-bottom: 0;
		background-color: #fff;
		margin-bottom: 16rpx;
		.shop-tuijian-title {
			font-size: 32rpx;
			height: 32rpx;
			line-height: 32rpx;
			color: #333;
			margin-bottom: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.shop-review-title {
			margin-top: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
	}
</style>